<?php 
namespace Home\Controller;
class DetailController extends HomeController
{
	public function index()
	{
		// var_dump($_SESSION);exit;
		$uid = $_SESSION['home_user']['id'];
		$id = I('get.id');
		// $id = 1;
		// var_dump($id);exit;
		$sql = "select id,language,country,duration,showdate,director,cid from lamp_movie where id=".$id;

		$data = M()->query($sql);
		// exit;
		//将电影中的分类ID字段转为数组
		$cid = explode(",",$data[0]['cid']);
		//遍历分类数组 将查询到的分类名存入$category数组中
		for ($i=0; $i < count($cid); $i++) { 
			$category[] = M('category')->field('name')->where('id='.$cid[$i])->select()[0]['name'];;
		}
		// var_dump($category);exit;
		//将得到的数组转换为字符串 存入$str中
		$cname = implode(',',$category);
		
		//获取演员的名字
		$sql = "select a.name from lamp_actor a join lamp_actor_movie am on a.id=am.aid where am.mid=".$id;
		$list = M()->query($sql);
		// var_dump($list);exit;
		//获得电影的封面
		$icon = M('img')->field('icon')->where('face=1 and mid='.$id)->select();
		// var_dump($id);
		// var_dump($icon);exit;
		//获得评论内容并分页
		//查询满足要求的总记录数
		$cont = M('comment');
		$count = $cont->field('content,u.name,u.icon')->join('lamp_user u on uid=u.id')->where('mid='.$id)->count();
		// var_dump($count);exit;
		//实例化分类页
		$Page = new \Think\Page($count,2);
		$Page->setConfig('first','首页'); 
		$Page->setConfig('prev','上一页');
		$Page->setConfig('next','下一页');
		$Page->setConfig('last','尾页');
		$show = $Page->show();//分页显示输出
		$comment = $cont->field('content,u.name,u.icon')->join('lamp_user u on uid=u.id')->where('mid='.$id)->limit($Page->firstRow.','.$Page->listRows)->order('addtime desc')->select();
		// var_dump($comment);exit;
		// var_dump($cid);
		// var_dump($icon);exit;
		// var_dump($list);exit;
		// var_dump($data);exit;
		if(!empty($uid)){
			$collection = M('collection')->where('uid='.$uid.' and mid='.$id)->select();
			$this->assign('collection',$collection);
		}
		// var_dump($collection);exit;
		$this->assign('uid',$uid);
		$this->assign('show',$show);
		$this->assign('user_photo',$_SESSION['home_user']['icon']);
		$this->assign('comment',$comment);
		$this->assign('cname',$cname);
		$this->assign('icon',$icon[0]);
		$this->assign('list',$list);
		$this->assign('data',$data['0']); 
		$this->display('index');
	}

	public function comment()
	{
		// var_dump($_POST);
		if(empty($_SESSION['home_user']))
		{
			$this->error('请先登入',U('Index/index'));
			exit;
		}
		if(empty($_POST['comment'])){
			$this->error('评论内容不能为空');
			exit;
		}
		//获得电影id
		$mid = I('get.id');
		//获得评论内容
		$comment = $_POST['comment'];
		//得到插入数据库的数组
		$com = array('mid'=>$mid,'uid'=>$_SESSION['home_user']['id'],'content'=>$comment);
		// var_dump($com);exit;
		$comm = D('comment');
		// exit;
		if(!$comm->create($com)){
			// exit;
			$this->error($comm->getError());
			exit;
		}else{
			if($comm->add() > 0){
				// exit;
				$this->success('评论完成');
			}else{
				$this->error('评论失败');
			}
		}

		// var_dump($com);exit;
		// var_dump($mid);exit;
	}
	public function collection()
	{
		// echo 'pig';
		$uid = I('post.uid');
		$mid = I('post.mid');
		$collection = D('collection');
		$id = $collection->where('uid='.$uid.' and mid='.$mid)->select();
		if(!empty($id)){
			if($collection->where('uid='.$uid.' and mid='.$mid)->delete() > 0){
				$data['bgc'] = "rgba(1,0,0,0.2)";
				$data['html'] = "点击收藏";
				$this->ajaxReturn($data);
				// echo '点击收藏';
			}
		}else{
			// echo $collection->create($_POST);
			if(!$collection->create($_POST)){
				// echo 'dog';
				$this->error($collection->getError());
			}else{
				if($collection->add() > 0){
					$data['bgc'] = "rgba(235,97,32,0.5)";
					$data['html'] = "已收藏"; 
					$this->ajaxReturn($data);
					// echo '已收藏';
				}
			}
		}
	}
}
























